package com.lzl.api.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.lzl.util.WebUtil;
import com.lzl.common.Pager;
import com.lzl.bean.my_dev.Restaurant;
import com.lzl.service.IRestaurantService;


/**
* 描述：Restaurant控制层
 * @author:cx
 * @version:1.0
 * @since:1.0
 * @createTime:2018-11-28 20:54:43
*/
@RestController
@RequestMapping(value = "/restaurants")
public class RestaurantController extends BaseController  {

    @Autowired
    private IRestaurantService restaurantService;

    @ApiOperation(value="详细信息")
    @RequestMapping(value = "/{id}", method = { RequestMethod.GET })
    public ResponseEntity<Object> getById(HttpServletRequest request,@PathVariable("id") Integer id) throws Exception {
		return new ResponseEntity<Object>(restaurantService.getById(id),HttpStatus.OK);
    }

    @ApiOperation(value="详细信息")
    @RequestMapping(value = "/random", method = { RequestMethod.GET })
    public ResponseEntity<Object> random(HttpServletRequest request) throws Exception {
        List<Restaurant> rests =  restaurantService.get((Map<String, Object>) request.getAttribute("QUERYMAP"),null);
        int result = (int)(Math.random()*(rests.size()));
        return new ResponseEntity<Object>(rests.get(result).getRestaurant(),HttpStatus.OK);
    }

    @ApiOperation(value="列表", notes="必传： ；选传：page,size")
    @RequestMapping(method = { RequestMethod.GET })
    public ResponseEntity<Object> get(HttpServletRequest request) throws Exception {
		Pager pager = WebUtil.handlerPager(request);
		return new ResponseEntity<Object>(restaurantService.get((Map<String, Object>) request.getAttribute("QUERYMAP"),pager),HttpStatus.OK);
    }
	
	@ApiOperation(value="新增", notes="必传： ；选传：")
    @RequestMapping(method = { RequestMethod.POST })
    public ResponseEntity<Object> post(HttpServletRequest request,@RequestBody Restaurant restaurant) throws Exception {
		restaurantService.post(restaurant);
		return new ResponseEntity<Object>(HttpStatus.CREATED);
    }
	
	@ApiOperation(value="更新", notes="必传：id ；选传：")
    @RequestMapping(method = { RequestMethod.PUT })
    public ResponseEntity<Object> put(HttpServletRequest request,@RequestBody Restaurant restaurant) throws Exception {
		restaurantService.put(restaurant);
		return new ResponseEntity<Object>(HttpStatus.OK);
    }

}